package com.example.demo.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.lang.Nullable;

public class DataSourceRoutingDataSource extends AbstractRoutingDataSource{

    private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceRoutingDataSource.class);

//    private static List<String> slaveKeys = new ArrayList<String>();
    /**
     * 获取数据源key
     * @return
     */
    @Nullable
    @Override
    protected Object determineCurrentLookupKey() {

        LOGGER.info(" 当前获取的key:{}",DataSourceManager.getType());
        if(DataSourceManager.getType().equals(DataSourceType.MASTER)){
            //主数据
            return DataSourceType.MASTER.getValue();
        }else{
            //从数据源
            return DataSourceType.SLAVE.getValue();
        }
    }

//    @Override
//    public void afterPropertiesSet() {
//
//
//    }
}
